//
//  TNToolCabinetViewController.m
//  TravelNotes
//
//  Created by 黄锋 on 15/8/10.
//  Copyright (c) 2015年 王浩宇. All rights reserved.
//

#import "TNToolCabinetViewController.h"
#import "MBProgressHUD.h"
#import "Macro.h"
#import "TNToolCabinetView.h"
#import "TNCityViewController.h"
#import "TNCurrentModel.h"
#import <AFNetworking.h>
#import "TNRateTableViewController.h"
#import "TNAccountTableViewController.h"
#import "TNSoundTranslateTableViewController.h"
#import "TNMapViewController.h"

#define kDestinationsInfos @"destinationsInfos"
#define kURL @"http://chanyouji.com/api/wiki/destinations/infos/*.json?page=1"

@interface TNToolCabinetViewController () <TNCityViewControllerDelegate, UIAlertViewDelegate>

@property(nonatomic, strong) UIButton *button; //rightbarbutton

@property(nonatomic, strong) MBProgressHUD *hud;  //大菊花
@property(nonatomic, strong) TNToolCabinetView *mainView;
@property(nonatomic, strong) TNCurrentModel *model;

@property(nonatomic, strong) TNSoundTranslateTableViewController *translateVC;
@property(nonatomic, strong) TNRateTableViewController *rateVC;
@property(nonatomic, strong) TNAccountTableViewController *accountVC;


@end

@implementation TNToolCabinetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0];
    self.navigationItem.title = @"旅行工具箱";
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:kDestinationsInfos];
    if (!number) {
        [self presentViewAction];
    } else {
        [self creatMainView];
    }
}

- (void)creatHud
{
    _hud = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
    [self.view addSubview:_hud];
    [_hud show:YES];
    [self parserData];
}

- (void)creatMainView
{
    _mainView = [[TNToolCabinetView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    __weak TNToolCabinetViewController *TNCVC = self;
    _mainView.toolCabinetBlock = ^(NSInteger item) {
        [TNCVC pushNextVCWithItem:item];
    };
    _mainView.tapblock = ^ {
        [TNCVC parserData];
    };
    [self.view addSubview:_mainView];
    NSString *cityName = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"];
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(SCREEN_WIDTH - 150, 10, 130, 40);
    _button.titleLabel.adjustsFontSizeToFitWidth = YES;
    _button.titleLabel.textAlignment = NSTextAlignmentRight;
    [_button setTitle:cityName forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(presentViewAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_button];
    [self creatHud];
}

//根据城市编号获取当地温度,时间
- (void)parserData
{
    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:kDestinationsInfos];
    if (number) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString *url = [kURL stringByReplacingOccurrencesOfString:@"*" withString:[number stringValue]];
        [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            _model = [[TNCurrentModel alloc] init];
            [_model setValuesForKeysWithDictionary:dic];
            _mainView.model = _model;
            [_hud removeFromSuperview];
            [_hud show:NO];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [_hud removeFromSuperview];
            [_hud show:NO];
            UIAlertView *view = [[UIAlertView alloc] initWithTitle:nil message:@"网络出错" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重试", nil];
            [view show];
        }];
    }
}

//国家,城市列表
- (void)presentViewAction
{
    TNCityViewController *cityVC = [[TNCityViewController alloc] init];
    cityVC.delegate = self;
    [self.navigationController pushViewController:cityVC animated:YES];
}

- (void)seleteCity
{
    if (_mainView) {
        [_button setTitle:[[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"] forState:UIControlStateNormal];
        [self creatHud];
    } else {
        [self creatMainView];
    }
}

//点击不同item进入相应界面
- (void)pushNextVCWithItem:(NSInteger)item
{
    switch (item) {
        case 0:
        {
            if (!_translateVC) {
                _translateVC = [[TNSoundTranslateTableViewController alloc] init];
            }
            _translateVC.language_code = _model.language_code;
            _translateVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:_translateVC animated:YES];
        }
            break;
        case 1:
        {
            _rateVC = [[TNRateTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
            _rateVC.model = _model;
            _rateVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:_rateVC animated:YES];
        }
            break;
        case 2:
        {
            _accountVC = [[TNAccountTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
            _accountVC.currency_display = _model.currency_display;
            _accountVC.self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:_accountVC animated:YES];
        }
            break;
        case 3:
        {
            TNMapViewController *mapVC = [[TNMapViewController alloc] init];
            mapVC.self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:mapVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self creatHud];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
